import api from '../../api';
import { message } from 'antd';
import { isZiGuanEvir } from '../../utils/commonFunc';

export default {
  namespace: 'tradeSearchM',

  state: {},
  effects: {
    //  建议书类型-枚举接口
    *etfproposalSelect({ payload, callback }, { call, put }) {
      const response = payload;
      if (response) {
        let typeArr = [];
        let statusArr = [];
        response.PROPOSAL_TYPE &&
          response.PROPOSAL_TYPE.forEach((item) => {
            typeArr.push({ title: item.value, value: item.code });
          });
        response.PROPOSAL_STATUS &&
          response.PROPOSAL_STATUS.forEach((item) => {
            statusArr.push({ title: item.value, value: item.code });
          });
        callback('success', { PROPOSAL_TYPE: typeArr, PROPOSAL_STATUS: statusArr, TRADE_BUS_STATUS: response.TRADE_BUS_STATUS });
      } else {
        callback('fail');
        message.error('获取账户类型下拉列表失败');
      }
    },

    // 获取建议书数据
    *getTradeData({ payload, callback }, { call, put }) {
      let withMAAT = false;
      let response;
      // 判断当前环境是不是资管
      let temp = isZiGuanEvir();
      if (!!temp && temp['iv-user']) withMAAT = true;
      if (withMAAT) {
        response = yield call(api.tradeSearch.getTradeDataWITHMAAT, payload);
      } else {
        response = yield call(api.tradeSearch.getTradeData, payload);
      }
      if (response.code == 0) {
        response.data.columns &&
          response.data.columns.splice(6, 0, {
            dataIndex: 'instructionStatus',
            key: 'instructionStatus',
            title: '指令状态',
          });
        callback('success', response.data);
      } else {
        callback('fail');
        message.error('获取账户类型下拉列表失败');
      }
    },
    // 撤回
    *cancelInstruction({ payload }, { call, put }) {
      let withMAAT = false;
      let response;
      // 判断当前环境是不是资管
      let temp = isZiGuanEvir();
      if (!!temp && temp['iv-user']) withMAAT = true;
      if (withMAAT) {
        response = yield call(api.tradeSearch.cancelInstructionWITHMAAT, payload);
      } else {
        response = yield call(api.tradeSearch.cancelInstruction, payload);
      }
      if (response.success) {
        message.success('撤回成功！');
      } else {
        message.error(response.errorMsg || '撤回失败');
      }
      return response;
    },
    //   获取明细数据
    *getDetailData({ payload, callback }, { call, put }) {
      let withMAAT = false;
      let response;
      // 判断当前环境是不是资管
      let temp = isZiGuanEvir();
      if (!!temp && temp['iv-user']) withMAAT = true;
      if (withMAAT) {
        response = yield call(api.tradeSearch.getDetailDataWITHMAAT, payload);
      } else {
        response = yield call(api.tradeSearch.getDetailData, payload);
      }
      if (response.success) {
        let tempObj = {};
        for (let k in response.data) {
          if (k == 'exchangecode') {
            tempObj['exchangeCode'] = response.data[k];
          } else {
            tempObj[k] = response.data[k];
          }
        }
        callback('success', tempObj);
      } else {
        callback('fail');
        message.error('获取明细数据失败');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
